import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from '../status/pinia/user'
import {ElMessage} from "element-plus";

const routes = [
  {
    path: '/',
    name: 'index',
    component: ()=>import('../views/index/Index.vue')
  },
  {
    path: '/index',
    name: 'home',
    component: ()=>import('../views/index/Index.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: ()=>import('../views/login/Login.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: ()=>import('@/views/Main/UserInfo.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: ()=>import('../views/login/Register.vue')
  },
  {
    path: '/accessories',
    name: 'accessories',
    component: ()=>import('../views/Main/Accessories.vue')
  },
  {
    path: '/solutions',
    name: 'solutions',
    component: ()=>import('../views/Main/Solutions.vue')
  },
  {
    path: '/resources',
    name: 'resources',
    component: ()=>import('../views/Main/Resources.vue')
  },
  {
    path: '/buy',
    name: 'buy',
    component: ()=>import('../views/Main/Buy.vue')
  }
]



const router = createRouter({
  history: createWebHistory(),
  routes
})


// 判断用户是否登录
router.beforeEach((to, form, next) => {
  if(!useUserStore.userinfo){
    if(to.path === '/*'){
      next('/login')
      return ElMessage({
        message: '请先登录！',
        type: 'warning',
        duration: 2000,
        center: true,
      })
    }
    else {
      next()
    }
  }
  else {
    next()
  }
})


export default router